# -*- coding: UTF-8 -*-

import os
import sys
import re
import argparse
import os.path
import glob
import fnmatch
import bs4

from bs4 import BeautifulSoup

DEFAULT_ENCODING = 'gb18030'
KEYWORDS_TO_DELETE = 'jpg'
CONVERT_HTML = ''

def getSubFileList(dir, pat=''):
    '''Get all file list with specified  suffix under current folder(Include sub folder)
    Input:  
        dir     :   Current folder
        suffix  :   default to blank, means select all files.
    Output:
        File list
    '''
    flist=[]
    for root, dirs, files in os.walk(dir):
        for name in files:
            if fnmatch.fnmatch(name, pat):
                flist.append(os.path.join(root,  name))
    return flist

def getCurrFileList(dir, pat=''):
    '''Get all file list with specified suffix under current level folder
    Input:  
        dir     :   Current folder
        suffix  :   default to blank, means select all files.
    Output:
        File list
    '''
    if pat=='':   
        files=glob.glob('*')
    else:
        files = []
        for file in os.listdir('.'):
            if fnmatch.fnmatch(file, pat):
                files += file        
        
    flist=[]    
    for f in files:
        flist.append(os.path.join(dir, f))
    return flist

def deleteFiles(dir, destDir, IsLoopSubDIR=True):
       
    if IsLoopSubDIR:
        flist=getSubFileList(dir, '*.html')
    else:
        flist=getCurrFileList(dir, '*.html')
    for fname in flist:
        if isPicPage(fname, KEYWORDS_TO_DELETE):
            os.remove(fname)
        
def isPicPage(fileName, keywords):    
    f = open(fileName, 'r', encoding=DEFAULT_ENCODING)
    content = f.read()
    jpgCount = content.count(keywords)
    if (jpgCount > 0):
        return True
    else:
        return False

def convertAllHtml(dir, IsLoopSubDIR=True):
    if IsLoopSubDIR:
        flist=getSubFileList(dir, '*.html')
    else:
        flist=getCurrFileList(dir, '*.html')
    for fname in flist:
        try:
            convertOneHtml(fname)
            print ('Success: ' + fname)
        except:
            print ('Error:   ' + fname)

def convertOneHtml(fileName):
    f = open(fileName, 'r', encoding=DEFAULT_ENCODING)
    content = f.read()
    newContent = content.replace('</html>', '')
    newContent = newContent.replace('&#8226;', ' ')
    newContent = newContent.replace('&nbsp;', ' ')    
    
    newContent += '</html>'
    f.close()
    
    # use bs4
    soup = BeautifulSoup(newContent)    

    # for 355aa
    newContent = ''    
    newFileName = fileName.replace('.html', '.txt')
    
    if os.access(newFileName, os.R_OK):
        os.remove(newFileName)
    
    f = open(newFileName, 'a', encoding=DEFAULT_ENCODING)

    # file content
    newContent = soup.get_text()
    f.write('\n' + newContent)    
        
    f.close()

if __name__ == '__main__':
    
    argument_parser = argparse.ArgumentParser(description='htm tool')
    argument_parser.add_argument('-i', dest = 'srcDir', help='the source folder name')
    argument_parser.add_argument('-o', dest = 'destDir', help='the destination folder name')
    argument_parser.add_argument('-d', dest = 'keywords', help='Delete the file which has the keywords')
    argument_parser.add_argument('-cf', dest = 'convertFile', help='Convert the specified html file')
    argument_parser.add_argument('-ca', dest = 'convertAll', action='store_true', help='Convert all html file')
    
    args = argument_parser.parse_args()  
    
    srcDir = args.srcDir
    destDir = args.destDir
    srcFile = args.convertFile
    KEYWORDS_TO_DELETE = args.keywords
        
    if (srcDir == None):
        srcDir = os.getcwd()

    if args.keywords != None:
        deleteFiles(srcDir, destDir)
        
    if args.convertFile != None:
        convertOneHtml(srcFile)
        
    if args.convertAll:
        convertAllHtml(srcDir)
    